#include <stdio.h>

int islandPerimeter(int **grid, int gridSize, int *gridColSize)
{
    int perimeter = 0;
    int row = gridSize;
    int col = *gridColSize;

    // 遍历网格中的每个格子
    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < col; ++j)
        {
            if (grid[i][j] == 1)
            { // 只有当遇到陆地格子时
                // 检查四个方向
                if (i == 0 || grid[i - 1][j] == 0)
                    perimeter++; // 上
                if (i == row - 1 || grid[i + 1][j] == 0)
                    perimeter++; // 下
                if (j == 0 || grid[i][j - 1] == 0)
                    perimeter++; // 左
                if (j == col - 1 || grid[i][j + 1] == 0)
                    perimeter++; // 右
            }
        }
    }

    return perimeter;
}

int main()
{
    int grid[4][4] = 
    {
        {0, 1, 0, 0},
        {1, 1, 1, 0},
        {0, 1, 0, 0},
        {1, 1, 0, 0}
    };

    int *gridPtr[4];
    for (int i = 0; i < 4; i++)
    {
        gridPtr[i] = grid[i];
    }

    int gridColSize = 4;
    int perimeter = islandPerimeter(gridPtr, 4, &gridColSize);
    printf("岛屿的周长是: %d\n", perimeter);

    return 0;
}